package services

import (
	"gin-app/app/common/request"
	"gin-app/app/models"
	"gin-app/global"
)

type bookService struct {
}

var BookService = new(bookService)

func (bs bookService) CreateBook(params request.CreateBook, userId string) (err error, book *models.Book) {
	book = models.NewBook(params.BookName, userId)
	err = global.App.DB.Create(book).Error
	return

}
func (bs bookService) GetBookList() (err error, books []models.Book) {
	err = global.App.DB.Find(&books).Error
	return

}

func (bs bookService) GetBook(params request.GetBook) (err error, books []models.Book) {
	err = global.App.DB.Where("book_id = ?", params.BookId).First(&books).Error
	return

}

// 现在只能通过书名查询书籍
func (bs bookService) QueryBook(params request.QueryBook) (err error, books []models.Book) {
	//db := global.App.DB
	//db.Table("(?) as u", db.Model(&models.User{}).Select("name")).Select("id").Where("name LIKE  ?", params.Keyword).First(&models.User{})
	err = global.App.DB.Where("book_name  LIKE ?", "%"+params.Keyword+"%").Find(&books).Error
	return

}
